package com.ocom.xcxapi.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.ocom.common.entity.card.EntityXfSignNo;
import com.ocom.common.request.xcx.PhoneUserVo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.HttpUtils;
import com.ocom.xcxapi.client.PayConfigClientService;
import com.ocom.xcxapi.vo.WxUserInfoVo;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2022/7/3
 **/
@Slf4j
@Component
public class ActiveQuiryConsumer {


	@Autowired
	PayConfigClientService payConfigClientService;

	@RabbitHandler
	@RabbitListener(queues = "dead-wxsign-check-delay-queue")
	public void listenDelayQueue(Message message, @Headers Map<String, Object> headers, Channel channel) throws IOException {

		System.out.println("延时队列接收到消息:" + message);
		try {
			Gson gson=new Gson();
			String se = new String(message.getBody(), "UTF-8");
			PhoneUserVo phoneUserVo= gson.fromJson(se,PhoneUserVo.class);
			System.out.println("延时消息"+phoneUserVo);

			String url = "http://192.168.3.123:32880/ocom_pay/offlineFacepay/user/select.do";

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("company_id", phoneUserVo.getCompany_id());
			jsonObject.put("user_name", phoneUserVo.getUser_name());
			jsonObject.put("password", phoneUserVo.getPassword());
			jsonObject.put("mchId", "");
			jsonObject.put("out_user_id", phoneUserVo.getOut_user_id());
//			jsonObject.put("out_user_id", phoneUserVo.getPId());
			String ss = HttpUtils.doPostBodyData(url, new HashMap<>(), jsonObject);

			Result result = JSONObject.parseObject(ss,Result.class);
			if(result.getCode()==200){
				WxUserInfoVo wxUserInfoVo = JSONObject.parseObject(JSON.toJSONString(result.getData()),WxUserInfoVo.class);
				log.info("微信人脸签约查询结果1 【{}】 ",wxUserInfoVo);
				//签约
				EntityXfSignNo entityXfSignNo =new EntityXfSignNo();
				entityXfSignNo.setComId(phoneUserVo.getComId());
				entityXfSignNo.setMerchId(0);
				entityXfSignNo.setPayWay(16);
				entityXfSignNo.setPId(phoneUserVo.getPId());
				if(wxUserInfoVo.getFace_image_ok()){
					entityXfSignNo.setSignNo(wxUserInfoVo.getContract_id());
					payConfigClientService.addUserSign(entityXfSignNo);
				}else{
					payConfigClientService.delUserSign(entityXfSignNo);
				}
			}
			log.info("微信人脸签约查询结果2 【{}】【{}】 ",ss);

		}catch (Exception e){
			log.info("微信人脸签约查询队列异常 【{}】 ",e);
		}
		//确认接收消息
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}
}
